package com.kang.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.kang.dao.PurchaseOrderItemMapper;
import com.kang.dao.PurchaseOrderMapper;
import com.kang.dao.PurchaseRequestItemMapper;
import com.kang.dao.SupplyAgreementItemMapper;
import com.kang.pojo.PurchaseItem;
import com.kang.pojo.PurchaseOrder;
import com.kang.pojo.PurchaseOrderItem;
import com.kang.pojo.SupplyAgreementItem;
import com.kang.service.PurchaseOrderService;
import com.vonechina.entity.page.PurchaseOrderPage;

/**
 * 采购订单服务层实现类
 * 
 * @author zs614
 *
 */
@Service
public class PurchaseOrderServiceImpl implements PurchaseOrderService {
	/**
	 * 采购订单持久层
	 */
	@Resource
	private PurchaseOrderMapper orderMapper;
	/**
	 * 采购订单物品信息持久层
	 */
	@Resource
	private PurchaseOrderItemMapper itemMapper;
	/**
	 * 采购申请物品信息持久层
	 */
	@Resource
	private PurchaseRequestItemMapper requestItemMapper;
	/**
	 * 供货协议物品信息持久层
	 */
	@Resource
	private SupplyAgreementItemMapper saim;

	/**
	 * 新增采购订单
	 * 
	 * @param order
	 *            采购订单实体类
	 * @return
	 */
	@Override
	public int insertOrder(PurchaseOrder order) {
		return orderMapper.insertOrder(order);
	}

	/**
	 * 生成采购订单编号
	 * 
	 * @return
	 */
	@Override
	public String generatoOrderNo() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String text = sdf.format(new Date()) + "0001";
		Long no = Long.parseLong(text);
		String max = orderMapper.selectMaxOrder();
		Long l = 0l;
		if (max != null) {
			l = Long.parseLong(max.replaceAll("[a-zA-Z]", ""));
		}
		if (l >= no) {
			no = l + 1;
		}
		return "CG" + no;
	}

	/**
	 * 保存采购订单物品信息
	 * 
	 * @param orderId
	 *            采购订单ID
	 * @param sourceID
	 *            物品来源ID
	 * @param itemNumber
	 *            采购数量
	 * @return
	 */
	@Override
	public int saveOrderItem(Integer orderId, Integer[] sourceID, Integer[] itemNumber) {
		itemMapper.updateRequestItemStatus(orderId, 0);
		itemMapper.deleteNotIn(orderId, sourceID);
		for (int i = 0; i < sourceID.length; i++) {
			PurchaseItem pItem = requestItemMapper.selectItemByIdAndStatus(sourceID[i], 0);
			PurchaseOrderItem item = itemMapper.selectOrderItem(orderId, sourceID[i]);
			if (item == null) {
				item = new PurchaseOrderItem();
				item.setPurchaseOrderID(orderId);
				item.setItemInformationID(pItem.getItemInformationID());
				item.setSourceID(sourceID[i]);
				item.setNumber(itemNumber[i]);
				itemMapper.addOrderItem(item);
			} else {
				item.setNumber(itemNumber[i]);
				itemMapper.updateOrderItem(item);
			}
		}
		return itemMapper.updateRequestItemStatus(orderId, 1);
	}

	/**
	 * 分页查询采购订单
	 * 
	 * @param page
	 *            搜索条件实体类
	 * @return
	 */
	@Override
	public List<PurchaseOrder> listPurchaseOrder(PurchaseOrderPage page) {
		page.setRows(orderMapper.countPurchaseOrder(page));
		return orderMapper.listPurchaseOrder(page);
	}

	/**
	 * 删除采购订单
	 * 
	 * @param purchaseOrderID
	 *            采购订单ID
	 * @return
	 */
	@Override
	public Integer deleteOrder(Integer purchaseOrderID) {
		return orderMapper.deleteOrder(purchaseOrderID);
	}

	/**
	 * 删除采购订单物品信息
	 * 
	 * @param purchaseOrderID
	 *            采购订单ID
	 * @return
	 */
	@Override
	public Integer deleteOrderAllItem(int purchaseOrderID) {
		List<PurchaseOrderItem> items = itemMapper.listItemForOrder(purchaseOrderID);
		for (PurchaseOrderItem item : items) {
			requestItemMapper.updateStatus(item.getSourceID(), 0);
			itemMapper.deleteOrderItem(item.getId());
		}
		return 1;
	}

	/**
	 * 查询采购订单物品信息
	 * 
	 * @param purchaseOrderID
	 *            采购订单ID
	 * @return
	 */
	@Override
	public List<PurchaseOrderItem> listItemForOrder(int purchaseOrderID) {
		return itemMapper.listItemForOrder(purchaseOrderID);
	}

	/**
	 * 提交采购订单
	 * 
	 * @param purchaseOrderID
	 *            采购订单ID
	 * @param submitterID
	 *            提交人ID
	 * @return
	 */
	@Override
	public Integer submitOrder(Integer purchaseOrderID, Integer submitterID) {
		return orderMapper.submitOrder(purchaseOrderID, submitterID);
	}

	/**
	 * 计算采购订单金额
	 * 
	 * @param order
	 *            采购订单实体类
	 * @return
	 */
	@Override
	public Double countOrderPrice(PurchaseOrder order) {
		double price = 0.0;
		List<PurchaseOrderItem> list = itemMapper.listItemForOrder(order.getPurchaseOrderID());
		for (PurchaseOrderItem item : list) {
			SupplyAgreementItem agreementItem = saim.selectItemBySupplier(order.getSupplierID(),
					item.getItemInformationID());
			price += item.getNumber() * agreementItem.getPurchaseprice();
		}
		return price;
	}

	/**
	 * 结算采购订单
	 * 
	 * @param order
	 *            采购订单实体类
	 * @return
	 */
	@Override
	public Integer settleOrder(PurchaseOrder order) {
		return orderMapper.settleOrder(order);
	}

	/**
	 * 根据purchaseOrderID查询出purchaseorder
	 * 
	 * @param purchaseOrderID
	 *            采购订单ID
	 * @return
	 */
	@Override
	public PurchaseOrder selectOrderById(Integer purchaseOrderID) {
		return orderMapper.selectOrderById(purchaseOrderID);
	}

	/**
	 * 更新采购订单
	 * 
	 * @param order
	 *            采购订单实体类
	 * @return
	 */
	@Override
	public Integer updateOrder(PurchaseOrder order) {
		return orderMapper.updateOrder(order);
	}

	/**
	 * 更新采购订单状态
	 * 
	 * @param purchaseOrderID
	 *            采购订单ID
	 * @param status
	 *            状态
	 * @return
	 */
	@Override
	public Integer updateStatus(Integer purchaseOrderID, Integer status) {
		return orderMapper.updateStatus(purchaseOrderID, status);
	}

	/**
	 * 查询采购订单表，用在采购入库添加订单
	 * 
	 * @param page
	 *            搜索条件实体类
	 * @return
	 */
	@Override
	public List<PurchaseOrder> listPurchaseOrder2(PurchaseOrderPage page) {
		return orderMapper.listPurchaseOrder2(page);
	}

	/**
	 * 查询采购订单表总条数，用在采购入库添加订单
	 * 
	 * @param page
	 *            搜索条件实体类
	 * @return
	 */
	@Override
	public Integer countPurchaseOrder2(PurchaseOrderPage page) {
		return orderMapper.countPurchaseOrder2(page);
	}

	/**
	 * 根据采购订单id查询出订单里面的物品
	 * 
	 * @param purchaseOrderItem
	 *            采购申请物品信息
	 * @return
	 */
	@Override
	public List<PurchaseOrderItem> selectPurchaseorderIteminformationByPurchaseOrderID(
			PurchaseOrderItem purchaseOrderItem) {
		return itemMapper.selectPurchaseorderIteminformationByPurchaseOrderID(purchaseOrderItem);
	}

	/**
	 * 根据id修改updatePurchaseorder_iteminformationById
	 * 
	 * @param purchaseOrderItem
	 *            采购订单物品信息
	 * @return
	 */
	@Override
	public int updatePurchaseorder_iteminformationById(PurchaseOrderItem purchaseOrderItem) {
		return itemMapper.updatePurchaseorder_iteminformationById(purchaseOrderItem);
	}

	/**
	 * 根据采购订单id查询出订单里面的物品
	 * 
	 * @param purchaseOrderID
	 *            采购订单ID
	 * @param itemIds
	 *            物品ID
	 * @return
	 */
	@Override
	public List<PurchaseOrderItem> selectPurchaseorderIteminformationByPurchaseOrderID2(Integer purchaseOrderID,
			Integer[] itemIds) {
		return itemMapper.selectPurchaseorderIteminformationByPurchaseOrderID2(purchaseOrderID, itemIds);
	}

	/**
	 * 根据id查询出采购订单物品信息
	 * 
	 * @param id
	 *            采购订单物品ID
	 * @return
	 */
	@Override
	public PurchaseOrderItem selectPurchaseorder_iteminformationById(Integer id) {
		return itemMapper.selectPurchaseorder_iteminformationById(id);
	}

	/**
	 * 根据入库物品信息查询采购订单信息
	 * 
	 * @param id
	 *            采购入库物品信息ID
	 * @return
	 */
	@Override
	public PurchaseOrder selectOrderById2(Integer id) {
		return orderMapper.selectOrderById2(id);
	}


}
